#ifndef PROTOCOL_H
#define PROTOCOL_H

#include <cstdint>
#include <QDebug>
#include <QByteArray>
#include "crc.h"
#include "protocol_define.h"

class protocol
{
public:
    protocol();
    ~protocol();
    void decode(uint8_t* data, uint16_t type, int size);    //解码函数
    void unpack(QByteArray data);                           //拆包函数
    /*****************************************共用************************************************/
    uint16_t protocol_get_type_length(enum protocol_type type);
    protocol_head protocol_head_pack(enum protocol_type type, uint32_t time);
    /*****************************************上位机->底盘******************************************/
    QByteArray protocol_pc_dipan_switch_pcak(uint8_t type, uint8_t control);
    QByteArray protocol_ackerman_pack(uint8_t mode, float v, float w, float r, float d);
    QByteArray protocol_reset_odom_pack(float x, float y, float d);
    QByteArray protocol_fine_pack(float vlf, float vrf, float vlb, float vrb, float dlf, float drf, float dlb, float drb);
    QByteArray protocol_motion_mode_pack(uint8_t mode);
    QByteArray protocol_cmd_pack(uint8_t cmd);
    QByteArray protocol_pc_dipan_time_sync_pack(void);
    QByteArray protocol_pc_dipan_heart_pack(uint8_t nav_pc, uint8_t img_pc);
    QByteArray protocol_pc_dipan_steer_engine_pack(uint8_t control);
    /*****************************************上位机->电源板****************************************/
    QByteArray protocol_switch_pack(uint8_t type, uint8_t control);
    QByteArray protocol_state_pack(uint8_t state);
    QByteArray protocol_pc_dianyuan_time_sync_pack(void);
    QByteArray protocol_pc_dianyuan_heart_pack(uint8_t nav_pc, uint8_t img_pc);
    QByteArray protocol_pc_dianyuan_func_set_pack(uint8_t id, uint8_t cmd);
    QByteArray protocol_pc_dianyuan_charge_id_pack(uint8_t charge_id);


public:
#define SUM_ENUM 64
    uint16_t frequency_count[SUM_ENUM];
    uint16_t frequency[SUM_ENUM];
    void update_frequency();
    /*****************************************底盘->上位机*****************************************/
    protocol_dipan_pc_heart         data_dipan_pc_heart;
    protocol_dipan_pc_ult           data_dipan_pc_ult;
    protocol_dipan_pc_other         data_dipan_pc_other;
    protocol_dipan_pc_encoder       data_dipan_pc_encoder;
    protocol_dipan_pc_speed         data_dipan_pc_speed;
    protocol_dipan_pc_reply         data_dipan_pc_reply;
    protocol_dipan_pc_imu           data_dipan_pc_imu;
    protocol_dipan_pc_status        data_dipan_pc_status;
    protocol_dipan_pc_alarm         data_dipan_pc_alarm;
    /*****************************************上位机->底盘*****************************************/
    protocol_pc_dipan_switch        data_pc_dipan_switch;
    protocol_pc_dipan_ackerman      data_pc_dipan_ackerman;
    protocol_pc_dipan_reset_odom    data_pc_dipan_reset_odom;
    protocol_pc_dipan_fine_control  data_pc_dipan_fine_control;
    protocol_pc_dipan_motion_mode   data_pc_dipan_motion_mode;
    protocol_pc_dipan_cmd           data_pc_dipan_cmd;
    protocol_pc_dipan_time_sync     data_pc_dipan_time_sync;
    protocol_pc_dipan_heart         data_pc_dipan_heart;
    protocol_pc_dipan_steer_engine  data_pc_dipan_steer_engine;
    /*****************************************电源板->上位机*****************************************/
    protocol_dianyuan_pc_heart      data_dianyuan_pc_heart;
    protocol_dianyuan_pc_battery    data_dianyuan_pc_battery;
    protocol_dianyuan_pc_status     data_dianyuan_pc_status;
    protocol_dianyuan_pc_reply      data_dianyuan_pc_reply;
    protocol_dianyuan_pc_radar      data_dianyuan_pc_radar;
    protocol_dianyuan_pc_radar      data_dianyuan_pc_radar_f;
    protocol_dianyuan_pc_radar      data_dianyuan_pc_radar_b;
    protocol_dianyuan_pc_func_flag  data_dianyuan_pc_func_flag;
    protocol_dianyuan_pc_alarm      data_dianyuan_pc_alarm;
    /*****************************************上位机->电源板*****************************************/
    protocol_pc_dianyuan_switch     data_pc_dianyuan_switch;
    protocol_pc_dianyuan_state      data_pc_dianyuan_state;
    protocol_pc_dianyuan_time_sync  data_pc_dianyuan_time_sync;
    protocol_pc_dianyuan_heart      data_pc_dianyuan_heart;
    protocol_pc_dianyuan_func_set   data_pc_dianyuan_func_set;
    protocol_pc_dianyuan_charge_id  data_pc_dianyuan_charge_id;
};

#endif // PROTOCOL_H
